﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
namespace Cine
{
    
    public partial class frmreserva : Form
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmreserva));

        butaca[,] matriz = new butaca[10,20];
        ArrayList reservas = new ArrayList();
        ArrayList reservas_auxiliar = new ArrayList();
        Sala sala = Sala.Deserialize<Sala>("Sala.xml");
        public frmreserva()
        {
            InitializeComponent();
            cargar_butacas(); 
        }

        
        
        public void cargar_butacas(){

             for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                   
                    matriz[i, j] = new butaca(i, j);
                    matriz[i, j].Width = 3;
                    matriz[i, j].Height = 1;
                    // matriz[i, j].Text = String.Format("{0},{1}", i, j);
                    matriz[i, j].Top = (i + 2) * 38;
                    matriz[i, j].Left = (j + 1) * 38;
                    matriz[i, j].BackColor = System.Drawing.SystemColors.ControlLightLight;
                    matriz[i, j].FlatAppearance.BorderColor = System.Drawing.Color.Black;
                    matriz[i, j].FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
                    matriz[i, j].FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                    matriz[i, j].FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                    
                    if (sala.verDisponibilidadButaca(i, j))
                    {
                       
                        //Console.Write(sala.butacaslista[i+j].ToString()); muestra todos los estados seguidos
                       

                        matriz[i, j].BackgroundImage = Image.FromFile(Application.StartupPath + @"\Imagenes\no_disponible.png");
                        matriz[i, j].Enabled = false;

                    }
                    else
                    {
                        
                        matriz[i, j].BackgroundImage = Image.FromFile(Application.StartupPath + @"\Imagenes\disponible.png");
                        matriz[i, j].Enabled = true;
                    }
                    
                   
                    
                    matriz[i, j].BackgroundImageLayout = ImageLayout.Stretch;
                    matriz[i, j].FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                    matriz[i, j].Size = new System.Drawing.Size(38, 37);
                    groupBox1.Controls.Add(matriz[i, j]);
                    matriz[i, j].Click += new System.EventHandler(reservar_butaca);

/*
                    matriz[i, j].BackColor = System.Drawing.SystemColors.ControlLightLight;
                    matriz[i, j].BackColor = System.Drawing.SystemColors.ControlLightLight;
                    matriz[i, j].FlatAppearance.BorderSize = 4;
                    matriz[i, j].FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
                    matriz[i, j].FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
                    matriz[i, j].FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                 // matriz[i, j].Image = ((System.Drawing.Image)(resources.GetObject("disponible.png")));
                    matriz[i, j].Image = ImageList.ImageCollection[1];
                    matriz[i, j].Top = i * 20;
                    matriz[i, j].Left = j * 40;
                    matriz[i, j].Name = "btn_200";
                    matriz[i, j].Size = new System.Drawing.Size(38, 37);
                    matriz[i, j].TabIndex = 199;
                    matriz[i, j].UseVisualStyleBackColor = false;
                    groupBox1.Controls.Add(matriz[i, j]);
                    matriz[i, j].Click += new System.EventHandler(marcar_vivo);
*/


                    

                }
            }
        }
    
           private void reservar_butaca(object sender, EventArgs e)
        {
              butaca butaca_marcada = ((butaca)sender);

            if (butaca_marcada.estaSeleccionado())
            {
                butaca_marcada.setDeseleccionar();
                sala.setEstadoButaca(butaca_marcada.getX(), butaca_marcada.getY());
                reservas_auxiliar.Remove(butaca_marcada.obtenerCoordenadas().ToString());
                butaca_marcada.BackgroundImage = Image.FromFile(Application.StartupPath + @"\Imagenes\disponible.png");
                butaca_marcada.BackgroundImageLayout = ImageLayout.Stretch;
                
                
            }
            else {
                butaca_marcada.setSeleccionado();
                sala.setEstadoButaca(butaca_marcada.getX(), butaca_marcada.getY());
                reservas_auxiliar.Add(butaca_marcada.obtenerCoordenadas().ToString());
                butaca_marcada.BackgroundImage = Image.FromFile(Application.StartupPath + @"\Imagenes\su_butaca.png");
                butaca_marcada.BackgroundImageLayout = ImageLayout.Stretch;
            }
           // MessageBox.Show(butaca_marcada.obtenerCoordenadas());
           
            
        }

           public class butaca : Button
           {
               int i;
               int j;
               private Boolean reservado = false;
               private Boolean seleccionado = false;
               public butaca(int i, int j)
               {
                   this.i = i;
                   this.j = j;
               }
               private void boton_click(object sender, EventArgs e)
               {
                   Console.WriteLine("I:" + i + "J:" + j);

               }
               public String obtenerCoordenadas()
               {
                   return i.ToString() + j.ToString();
               }
               public Boolean estaReservado()
               {
                   return reservado;
               }
               public Boolean estaSeleccionado()
               {
                   return seleccionado;
               }
               public int getX()
               {
                   return this.i;
               }
               public int getY()
               {
                   return this.j;
               }
               
               public void setSeleccionado() {
                   this.seleccionado = true;
               }
               public void setDeseleccionar()
               {
                   this.seleccionado = false;
               }
           }

           private void btnticket_Click(object sender, EventArgs e)
           {
             //  String nombre_variable_cualquiera = String.Join(",", reservas.ToArray());
              // MessageBox.Show(nombre_variable_cualquiera);

               String reservasStr = "";
               String coma = ", ";
               
               for (int i = 0; i < reservas_auxiliar.Count; i++)
               {
                   if (i == reservas_auxiliar.Count-1) {
                       coma = ".";
                   }
                       reservasStr = "" + reservasStr + reservas_auxiliar[i]+ coma ;
                       
                       
               }
              
              
               if(reservas_auxiliar.Count == 0){
                    MessageBox.Show("No ha seleccionado ninguna butaca","Atencion Usuario",MessageBoxButtons.OK,MessageBoxIcon.Error);
               }else{
                   if (MessageBox.Show(reservasStr, "Reserva", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.OK)
                   {
                       reservas = (ArrayList)reservas_auxiliar.Clone();
                       foreach (string r in reservas) { Console.WriteLine(r); }
                       this.Close();
                   }
                   else
                   {

                       
                   }
               }
           }

           private void btnsalir_Click(object sender, EventArgs e)
           {
               if (MessageBox.Show("¿Está seguro que desea Salir?", "Salir", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.Yes)
               {
                   this.Close();
               }
           }

           private void btncancelar_Click(object sender, EventArgs e)
           {
               foreach (string r in reservas_auxiliar) { reservas.Remove(r); }

               for (int i = 0; i < 10; i++)
               {
                   for (int j = 0; j < 20; j++)
                   {
                       
                       sala.butacas_inicial();
                       matriz[i, j].setDeseleccionar();
                       matriz[i, j].BackgroundImage = Image.FromFile(Application.StartupPath + @"\Imagenes\disponible.png");
                       matriz[i, j].BackgroundImageLayout = ImageLayout.Stretch;


                   }
               }
           }


    }
    
}

